import QtQuick
import QtQuick.Controls.Material
import QtQuick.Layouts

ApplicationWindow {
    id: mainWindow
    width: screenWidth
    height: screenHeight
    visible: true
    title: qsTr("Hello World")
    visibility: "FullScreen"
    x: Qt.application.screens[1].virtualX
    y: Qt.application.screens[1].virtualY

    property real screenWidth: 1920
    property real screenHeight: 1080

    Item{
        id: root
        anchors.fill: parent
    }

    // 当前页码:
    property int currentPage: 5

    Component.onCompleted: {
        currentPage = 1        
        pageLoader.source = "ManualProcessWindow.qml"
        // Cpp_PLC.enter_Auto_Process_Window();
    }

    Row {
        anchors.top: root.top
        anchors.topMargin: 25
        anchors.left: root.left
        anchors.leftMargin: 10
        spacing: 5

        // // 第一个导航按钮
        // A_Button {
        //     contentText_Data: qsTr("读取数据")
        //     // 根据是否选中设置样式
        //     isSelected: currentPage === 1
        //     onButtonClicked: {
        //         currentPage = 1
        //         pageLoader.source = "AutoProcessWindow.qml"
        //     }
        // }

        // 第二个导航按钮
        A_Button {
            contentText_Data: qsTr("手动加工")
            isSelected: currentPage === 2 || currentPage === 5
            onButtonClicked: {
                currentPage = 2
                pageLoader.source = "ManualProcessWindow.qml"                
            }
        }

        // 第三个导航按钮
        A_Button {
            contentText_Data: qsTr("型材信息")
            isSelected: currentPage === 3 ||
                        currentPage === 8 ||
                        currentPage === 9
            onButtonClicked: {
                currentPage = 3
                pageLoader.source = "MaterialListWindow.qml"
            }
        }

        // 第四个导航按钮
        A_Button {
            contentText_Data: qsTr("系统设置")
            isSelected: currentPage === 4 ||
                        currentPage === 10 ||
                        currentPage === 11 ||
                        currentPage === 12 ||
                        currentPage === 13 ||
                        currentPage === 14
            onButtonClicked: {
                currentPage = 4
                pageLoader.source = "SystemParameterWindow.qml"
            }
        }
        // 第五个导航按钮
        A_Button {
            contentText_Data: qsTr("退出")
            onButtonClicked: {
                console.log("触发退出!")
                Qt.quit()
            }
        }
    }

    Rectangle{
        anchors.top: root.top
        anchors.topMargin: 25
        anchors.left: root.left
        anchors.leftMargin: 840
        width: Cpp_PLC.ConnectState === 3 ? 160 : 120
        height: 50
        radius: 5
        border.width: 3
        border.color: "black"
        color: Cpp_PLC.ConnectState === 1 ? "green" :
               Cpp_PLC.ConnectState === 2 ? "red" :
                                            "gold"
        Text {
            font.pixelSize: 30
            anchors.centerIn: parent
            font.bold: true
            text: Cpp_PLC.ConnectState === 1 ? qsTr("已连接"):
                  Cpp_PLC.ConnectState === 2 ? qsTr("已断开") :
                                               qsTr("连接中...")
        }
    }

    Rectangle{
        anchors.top: root.top
        anchors.topMargin: 25
        anchors.left: root.left
        anchors.leftMargin: 980
        width: 200
        height: 50
        radius: 5
        border.width: 3
        border.color: "black"
        color: "red"
        visible: !Cpp_PLC.Stop_Signal
        Text {
            font.pixelSize: 30
            anchors.centerIn: parent
            font.bold: true

            text:  qsTr("急停报警中!!!")
        }
    }


    WarningList_Area{       // 报警显示列表
        anchors.top: root.top
        anchors.topMargin: 80
        anchors.left: root.left
        anchors.leftMargin: 650
    }

    A_Button{
        anchors.top: root.top
        anchors.topMargin: 80
        anchors.left: root.left
        anchors.leftMargin: 1290
        buttonWidth: 230
        buttonHeight: 50
        contentText_Data : qsTr("复位报警")
        onButtonPressed: Cpp_PLC.write_into_tcp_Hold16(6166,1)
        onButtonReleased: Cpp_PLC.write_into_tcp_Hold16(6166,0)
    }


    Loader{
        id: pageLoader
        anchors.top: root.top
        anchors.topMargin: 80
        anchors.horizontalCenter: root.horizontalCenter

        onStatusChanged: {
            switch(status) {
            case Loader.Null :{
                console.debug("加载空项");
                break;
            }
            case Loader.Ready:{
                console.info("加载完成: " + source);
                break;
            }
            case Loader.Loading :{
                console.debug("加载中: "+source);
                break;
            }
            case Loader.Error :{
                console.error("加载错误: "+ source);
                break;
            }
            }
        }

    }




}
